﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TVH.Entidade;
using TVH.Negocio;
using TVH.Web.Code;
using TVH.Comum;
using System.Web.Security;

namespace TVH.Web
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnTrocarSenha_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/TrocarSenha.aspx");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void LoginValidate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            try
            {
                Usuario usu = new NegUsuario().ObterLista(new Usuario()
                {
                    Login = txtLogin.Text
                }).FirstOrDefault();
                if (usu == null || usu.SenhaCriptografada != txtSenha.Text.Criptografar())
                {
                    args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                    Session.Usuario = usu;
                }
            }
            catch (Exception ex)
            {
                ExibirMensagemErro("Erro ao efetuar o login do usuário", ex);
            }
        }
    }
}